<?php

namespace app\controllers;

use app\components\StatisticsService;
use app\models\MobiNews;
use app\models\MobiNotice;
use Yii;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\components\ChinaService;
use yii\web\Response;

class InfoController extends BaseController
{
    protected $key = 'android-apk';
    protected $code = 'android-code';

    /**
     * 方法说明：站点首页
     *
     * 作者：张华杰 blues@lonlife.cn
     * 时间：2017-10-31 3:30 PM
     */
    public function actionIndex()
    {
        //渠道统计
        StatisticsService::channel();

        //判断访问设备和地区
        $isAndroid = false;
        if (false !== strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'android')) {
            $isAndroid = true;
        }
        $isChina = ChinaService::isChina();

        //获取文章，并拼接内页的进入链接
        $article = MobiNews::find()
            ->orderBy('created_at desc')
            ->limit(9)
            ->asArray()
            ->all();
        foreach ($article as $key => $value) {
            $article[$key]['url'] = '/news/'.$article[$key]['id'];
        }

        //ＰＣ端，手机端渲染不同ｖｉｅｗ
        if ($this->is_mobile_request()) {
            return $this->render('index_wap', [
                'isChina' => $isChina,
                'isAndroid' => $isAndroid
            ]);
        }
        return $this->render('index', [
            'article' => $article,
            'isChina' => $isChina,
        ]);
    }

    /**
     * 游戏资讯外页
     */
    public function actionNews()
    {
        $data = MobiNews::find()
            ->orderBy('created_at desc');
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => 5]);
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();

        if ($this->is_mobile_request()) {
            return $this->render('news_wap', [
                    'model' => $model,
                    'pages' => $pages,
                ]
            );
        }
        return $this->render('news', [
                'model' => $model,
                'pages' => $pages,
            ]
        );
    }

    public function actionNewsDetail($id)
    {
        $model = MobiNews::find()->where(['id' => $id])->one();
        $pre_model = MobiNews::find()->where(['<', 'id', $id])->orderBy(['id' => SORT_DESC])->one();
        $next_model = MobiNews::find()->where(['>', 'id', $id])->orderBy(['id' => SORT_ASC])->one();

        if ($this->is_mobile_request()) {
            return $this->render('news_detail_wap', [
                'model' => $model,
                'pre_model' => $pre_model,
                'next_model' => $next_model
            ]);
        }
        return $this->render('news_detail', [
            'model' => $model,
            'pre_model' => $pre_model,
            'next_model' => $next_model
        ]);
    }


    public function actionNotice()
    {
        $data = MobiNotice::find()
            ->orderBy('created_at desc');
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => 5]);
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();

        if ($this->is_mobile_request()) {
            return $this->render('notice_wap', [
                    'model' => $model,
                    'pages' => $pages,
                ]
            );
        }
        return $this->render('notice', [
                'model' => $model,
                'pages' => $pages,
            ]
        );
    }

    public function actionNoticeDetail($id)
    {
        $model = MobiNotice::find()->where(['id' => $id])->one();
        $pre_model = MobiNotice::find()->where(['<', 'id', $id])->orderBy(['id' => SORT_DESC])->one();
        $next_model = MobiNotice::find()->where(['>', 'id', $id])->orderBy(['id' => SORT_ASC])->one();

        if ($this->is_mobile_request()) {
            return $this->render('notice_detail_wap', [
                'model' => $model,
                'pre_model' => $pre_model,
                'next_model' => $next_model
            ]);
        }
        return $this->render('notice_detail', [
            'model' => $model,
            'pre_model' => $pre_model,
            'next_model' => $next_model
        ]);
    }



    public function actionAbout()
    {
        if ($this->is_mobile_request()) {
            return $this->render('about_wap');
        }
        return $this->render('about');
    }

}
